package testing.observer
{
	public class ObsConcreteSubject implements ObsSubject
	{
		
		private var light:String;
		private var observers:Array;
		private var isduplicate:Boolean;
		
		public function ObsConcreteSubject()
		{
			trace("--> concrete subject");
			observers = new Array();
		}
		
		public function subscribeObserver(obserNow:ObsObserver):void {
			isduplicate = false;
			for(var ob:int = 0; ob < this.observers.length; ob++) {
				if(this.observers[ob] == obserNow) {
					isduplicate = true;
					trace(this.observers[ob].subName + ": multiple subscription denied!\n");
				}
			}
			if(!isduplicate) 
				observers.push(obserNow);
		}
		
		public function unsubscribeObserver(obserNow:ObsObserver):void {
			for(var ob:int = 0; ob < observers.length; ob++) {
				if(observers[ob] == obserNow) {
					observers.splice(ob, 1);
					break;
				}
			}
		}
		
		public function notifyObserver():void {
			for(var notify:* in observers) {
				observers[notify].update(light);
				trace("Observer" + notify + " Light is " + light);
			}
		}
		
		public function setLight(light:String):void {
			this.light = light;
			notifyObserver();
		}
		

	}
}